package systemapi

import (
	"github.com/gin-gonic/gin"
	"iot-base/common/config"
	"iot-base/common/webapi"
)

func SetCfg(cfg config.CONFIG) {
	config.SaveByNewCfg(cfg)
}

func GetCfg() config.CONFIG {
	return config.GetAllConfig()
}

//{mqttAddr,mqttUser,mqttUser,redisUser,redisPassword,dbPassword,name}

func initSystemAPI(cfg *gin.RouterGroup) {
	g := cfg.Group("/system")
	{
		g.GET("/", func(c *gin.Context) {
			cfg := GetCfg()
			option := make(map[string]string)
			option["mqttAddr"] = cfg.MQTTOption.Addr
			option["mqttUser"] = cfg.MQTTOption.UN
			option["mqttPassword"] = cfg.MQTTOption.PWD

			option["redisUser"] = cfg.DBOption.RedisOption.UserName
			option["redisPassword"] = cfg.DBOption.RedisOption.Password

			option["dbPassword"] = cfg.DBOption.Password
			option["name"] = cfg.SelfOption.Name
			option["ID"] = cfg.SelfOption.ID

			webapi.SendResult(option, nil, c)

		})
		g.PUT("/", func(c *gin.Context) {
			cfg := GetCfg()
			c.BindJSON(&cfg)

			SetCfg(cfg)
			webapi.SendOK(c)

		})
	}
}
